var gpTimeout;

var cammandJMRoom = function(result) {
	var data = result.data;
	if (typeof (data) == "undefined") {
		return;
	}

	$("#mj").empty();
	$("#mmj").empty();
	$("#topmj").empty();
	$("#topmmj").empty();
	$("#rightmj").empty();
	$("#rightmmj").empty();
	$("#leftmj").empty();
	$("#leftmmj").empty();
	$("#centermj").empty();
	$("#play").empty();

	// 自己
	var mj = getUserMJ(0, data.usermj);
	var mmj = getUserMJ(0, data.usermmj);
	if ((mj.length + (mmj.length * 3)) > 13) {
		$("#play")
				.append(
						'<button type="button" onClick="MJ1003(playmj())" class="btn">出牌</button>');
	}
	for (var i = 0; i < mj.length; i++) {
		$("#mj").append('<div class="majiang clickmj">' + mj[i] + '</div>');
	}

	for (var i = 0; i < mmj.length; i++) {
		$("#mmj").append('<div class="mmajiang">' + mmj[i] + '</div>');
	}
	// 上
	var mj = getUserMJ(2, data.usermj);
	for (var i = 0; i < mj.length; i++) {
		$("#topmj").append('<div class="majiang">' + mj[i] + '</div>');
	}
	var mmj = getUserMJ(2, data.usermmj);
	for (var i = 0; i < mmj.length; i++) {
		$("#topmmj").append('<div class="mmajiang">' + mmj[i] + '</div>');
	}

	// 右
	var mj = getUserMJ(1, data.usermj);
	for (var i = 0; i < mj.length; i++) {
		$("#rightmj").append('<div class="lrmajiang">' + mj[i] + '</div>');
	}
	var mmj = getUserMJ(1, data.usermmj);
	for (var i = 0; i < mmj.length; i++) {
		$("#rightmmj").append('<div class="lrmmajiang">' + mmj[i] + '</div>');
	}

	// 左
	var mj = getUserMJ(3, data.usermj);
	for (var i = 0; i < mj.length; i++) {
		$("#leftmj").append('<div class="lrmajiang">' + mj[i] + '</div>');
	}
	var mmj = getUserMJ(3, data.usermmj);
	for (var i = 0; i < mmj.length; i++) {
		$("#leftmmj").append('<div class="lrmmajiang">' + mmj[i] + '</div>');
	}

	// 中
	var mj = data.centermj;
	$("#centermj").append(
			'<div>' + "当前剩余:" + data.pondmj.length + "张牌，我是后扣:" + data.houkou
					+ '</div>');
	$("#centermj").append('<div>' + "当前出的牌:" + data.playmj + '</div>');
	for (var i = 0; i < mj.length; i++) {
		$("#centermj").append('<div class="cmajiang">' + mj[i] + '</div>');
	}

	new clickmj();// 选中事件
}

function getUserMJ(no, mj) {
	var usernonow = userno + no;
	usernonow = usernonow % 4;

	if (usernonow == 1) {
		return mj.u1;
	} else if (usernonow == 2) {
		return mj.u2;
	} else if (usernonow == 3) {
		return mj.u3;
	} else if (usernonow == 4 || usernonow == 0) {
		return mj.u4;
	}
}

var cammandMJ1003 = function(result) {
	var data = result.data;
	var palytype = data.palytype;

	var mmj = playpeng(data.playmj, getUserMJ(0, data.usermj));
	for (var i = 0; i < mmj.length; i++) {
		$("#gangpeng").append(
				"<button type='button' onClick='MJ1004(" + '"' + mmj[i] + '"'
						+ ")'>" + mmj[i] + "</button>");
	}
	gpTimeout = window.setTimeout(function() {
		gangpeng(data);
	}, 5000);// 使用字符串执行方法

	cammandJMRoom(result)
}

var cammandMJ1004 = function(result) {
	var data = result.data;
	var palytype = data.palytype;
	$("#play").empty();
	cammandJMRoom(result)
}
var cammandMJ1005 = function(result) {
	var data = result.data;
	var palytype = data.palytype;
	$("#play").empty();
	cammandJMRoom(result)
}

var cammand8888 = function(result) {
	// output(JSON.stringify(data));
}

var cammand9999 = function(result) {
	alert(result.msg);
}

var cammandtest = function(result) {
	// output(JSON.stringify(data));
}

var jsonObject = [ {
	fun : cammandJMRoom,
	command : "MJ1001"
}, {
	fun : cammandJMRoom,
	command : "MJ1002"
}, {
	fun : cammandMJ1003,
	command : "MJ1003"
}, {
	fun : cammandMJ1004,
	command : "MJ1004"
}, {
	fun : cammandMJ1005,
	command : "MJ1005"
}, {
	fun : cammand8888,
	command : "8888"
}, {
	fun : cammand9999,
	command : "9999"
}, {
	fun : cammandtest,
	command : "test"
} ];
function initMajiangSocket(url) {
	initSocket(url, jsonObject, "majiang");
}
